<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Resources\Estate\UserResource\UserCollection;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @param User $user_model
     * @return array|Response
     */
    public function index(Request $request, User $user_model)
    {
        $where = [];
        if (isset( $request->username ) && !empty( $request->username )) {
            $where[] = ['username','like',"%{$request->username}%"];
        }
        if (isset( $request->phone ) && !empty( $request->phone )) {
            $where[] = ['phone','=',$request->phone];
        }
        if (isset( $request->community_id ) && !empty( $request->community_id )) {
            $where[] = ['community_id','=', $request->community_id ];
        }

        $list = $user_model->where($where)->orderBy('id', 'desc')->paginate($request->per_page ?? 30);

        return $this->success(new UserCollection($list));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @param User $user_model
     * @return array|Response
     */
    public function store(Request $request, User $user_model)
    {
        if ($user_model->where('username', $request->username)->exists()) {
            return $this->error(__('admins.username_existence'));
        }
        if ($user_model->where('phone', $request->phone)->exists()) {
            return $this->error(__('admins.phone_existence'));
        }

        $user_model = $user_model->create([
            'username' => $request->username,
            'phone'    => $request->phone,
            'password' => Hash::make($request->password ?? '123456'),
            'nickname' => $request->nickname ?? '神秘人',
            'avatar'   => $request->avatar ?? '',
            'community_id' => $request->community_id,
            'integral' => $request->integral
        ]);

        return $this->success([], __('base.success'));
    }

    /**
     * Display the specified resource.
     *
     * @param User $user_model
     * @param int $id
     * @return Response
     */
    public function show(User $user_model, int $id): Response
    {
        $info = $user_model->find($id);
        return $this->success($info);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param User $user_model
     * @param int $id
     * @return array|Response
     */
    public function update(Request $request, User $user_model, int $id)
    {
        if ($user_model->where('username', $request->username)
            ->where('id', '<>', $id)
            ->exists()
        ) {
            return $this->error(__('admins.username_existence'));
        }
        if ($user_model->where('phone', $request->phone)
            ->where('id', '<>', $id)
            ->exists()
        ) {
            return $this->error(__('admins.phone_existence'));
        }
        $user_model           = $user_model->find($id);
        $user_model->username = $request->username;
        if (!empty($request->password)) {
            $user_model->password = Hash::make($request->password ?? '123456');
        }
        $user_model->nickname = $request->nickname ?? '神秘人';
        $user_model->avatar   = $request->avatar ?? '';

        $user_model->community_id = $request->community_id;
        $user_model->integral = $request->integral;

        $user_model->save();
        return $this->success([], __('base.success'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param User $user_model
     * @param int $id
     * @return array|Response
     */
    public function destroy(User $user_model, int $id)
    {
        $idArray = array_filter(explode(',', $id), static function ($item) {
            return is_numeric($item);
        });

        $user_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    public function edit_status( Request $request, User $user_model): array
    {
        $id = $request->id;
        $user_model = $user_model->find($id);
        $user_model->status = $user_model->status == 1 ? 2 : 1;
        $user_model->save();
        return $this->success([], __('base.success'));
    }
}
